package com.lu.spzx.manager.config;

import com.lu.spzx.manager.interceptor.LoginAuthInterceptor;
import com.lu.spzx.manager.properties.UserAuthProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Component
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Autowired
    private LoginAuthInterceptor loginAuthInterceptor;

    @Autowired
    private UserAuthProperties userAuthProperties;

    // 跨域处理
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 添加路径规则
        registry.addMapping("/**")
                // 是否允许在跨域的情况下传递 Cookie
                .allowCredentials(true)
                // 允许请求来源的域规则
                .allowedOriginPatterns("*")
                // 允许所有请求方式
                .allowedMethods("*")
                // 允许所有的请求头
                .allowedHeaders("*");
    }

    // 登录校验拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginAuthInterceptor)
                // 拦截以下两个请求之外的所有请求
                .excludePathPatterns(userAuthProperties.getNoAuthUrls())
                .addPathPatterns("/**");
    }
}
